﻿
@{
    ViewBag.Title = "用户管理";
}
<blockquote class="layui-elem-quote quoteBox">
    <div class="layui-inline">
        <div class="layui-input-inline">
            <input type="text" name="RealName" data-op="cn" class="layui-input searchVal" placeholder="请输入用户姓名" />
        </div>
        <a class="layui-btn search_btn" data-type="reload">搜索</a>
    </div>
    <div></div>
</blockquote>

<table id="laytable" lay-filter="userList"></table>

@section script{
    <script type="text/javascript">
        layui.use(["tools", "laytpl"], function () {
            var $ = layui.$, layer = layui.layer, table = layui.table, tools = layui.tools, form = layui.form;
            table.render({
                elem: '#laytable',
                url: '/UserManage/User/Index',
                method: 'post',
                toolbar: '#toolsHead',
                loading: true,
                height: "full-125",
                limit: 15,
                limits: [10, 15, 20, 25, 30, 35, 40, 45, 50],
                page: true,
                where: { order: 'AccountId asc' },
                cols: [[
                    { type: 'numbers' },
                    { field: 'RealName', title: '姓名' },
                    { field: 'UserName', title: '账户名' },
                    { field: 'MobilePhone', title: '手机号码' },
                    { field: 'Email', title: '邮箱' }, {
                        field: 'EnabledMark', title: '账户状态', templet: function (d) {
                            return '<input type="checkbox" name="enabled" lay-filter="enabled" lay-skin="switch" key="' + d.AccountId + '" lay-text="启用|禁用" ' + (d.EnabledMark ? 'checked' : '') + '>'
                        }
                    },
                    {
                        field: 'Gender', title: '性别', templet: function (d) {
                            return d.Gender ? "男" : "女";
                        }
                    },
                    { field: 'Birthday', title: '出生日期' },
                    { title: '操作', fixed: 'right', align: 'center', hide: rowButton.length == 0, width: rowButton.length * 80, templet: '#toolsRow' }
                ]]
            });
            $(".search_btn").click(function () {
                table.reload("laytable", {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: tools.condition()
                });
            });

            table.on('tool(userList)', function (obj) {
                var url = $(this).data("url"), layEvent = obj.event;
                active[layEvent](url, obj.data.AccountId);
            });

            table.on('toolbar(userList)', function (obj) {
                var url = $(this).data("url");
                var layEvent = obj.event;
                active[layEvent](url);
            });

            form.on('switch(enabled)', function (data) {
                let _this = $(this), s = data.elem.checked;
                tools.submitConfrim({
                    prompt: "您确定需要" + (s ? "启用" : "禁用") + "当前用户吗？",
                    url: "/UserManage/User/Enable",
                    data: { id: _this.attr("key"), status: s },
                    cancelCallBack: true,
                    success: function (result) {
                        if (result.StatusCode === 200) {
                            _this.prop('checked', s);
                        } else {
                            _this.prop('checked', !s);
                        }
                        form.render("checkbox");
                    }, cancel: function () {
                        _this.prop('checked', !s);
                        form.render("checkbox");
                    }
                });
            });

            var active = {
                add: function (url) {
                    tools.moduleOpen({
                        title: "新增系统用户",
                        url: url,
                        width: "100%",
                        height: "100%",
                        callBack: function (index, layero) {
                            var iframeWindow = window["layui-layer-iframe" + index];
                            iframeWindow.layui.form.on("submit(save)", function (data) {
                                tools.submit({
                                    url: url,
                                    data: data.field,
                                    loading: "正在保存数据...",
                                    success: function () {
                                        table.reload("laytable");//重新加载
                                        layer.close(index);
                                    }
                                });
                            });
                            layero.find("iframe").contents().find("#btnSave").trigger("click");
                        }
                    });
                },
                edit: function (url, id) {
                    tools.moduleOpen({
                        title: "编辑系统用户",
                        url: url + "?key=" + id,
                        width: "100%",
                        height: "100%",
                        callBack: function (index, layero) {
                            var iframeWindow = window["layui-layer-iframe" + index];
                            iframeWindow.layui.form.on("submit(save)", function (data) {
                                tools.submit({
                                    url: url + "?AccountId=" + id,
                                    data: data.field,
                                    loading: "正在保存数据...",
                                    success: function () {
                                        table.reload("laytable");//重新加载
                                        layer.close(index);
                                    }
                                });
                            });
                            layero.find("iframe").contents().find("#btnSave").trigger("click");
                        }
                    });
                },
                delete: function (url, id) {
                    tools.submitConfrim({
                        prompt: "您确定需要删除当前用户吗？",
                        url: url,
                        data: { key: id },
                        success: function () {
                            table.reload("laytable");//重新加载
                        }
                    });
                }, reset: function (url, id) {
                    tools.moduleOpen({
                        title: "重置密码",
                        url: url,
                        width: "450px",
                        height: "230px",
                        callBack: function (index, layero) {
                            var iframeWindow = window["layui-layer-iframe" + index];
                            iframeWindow.layui.form.on("submit(save)", function (data) {
                                if (data.field.Password != data.field.RePassword) {
                                    tools.msg("两次输入的密码不一致");
                                    return false;
                                }
                                tools.submit({
                                    url: '/UserManage/User/Reset?AccountId=' + id,
                                    data: data.field,
                                    loading: "正在保存数据...",
                                    success: function () {
                                        table.reload("laytable");//重新加载
                                        layer.close(index);
                                    }
                                });
                            });
                            layero.find("iframe").contents().find("#btnSave").trigger("click");
                        }
                    });
                }
            };
        });
    </script>
}